package com.haidechizi.leetcode;

import java.util.ArrayList;
import java.util.List;

public class _22_括号生成 {

    public List<String> generateParenthesis(int n) {

        List<String> result = new ArrayList<>();
        generate("(", n - 1, n, result);
        return result;
    }

    public void generate(String str, int left, int right, List<String> result) {
        if (left == 0 && right == 0) {
            result.add(str);
            return;
        }
        if (left > 0) {
            generate(str + "(", left - 1, right, result);
        }
        if (left < right) {
            generate(str + ")", left, right - 1, result);
        }
    }

}
